/*
 * Copyright (c) 2021, 2022, yzfh.cn All rights reserved.
 *
 */
package com.ly.proxy.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

/**
 * @author yzfh
 * @Date 2022/02/21 星期一 11:34:24
 */
public class ProxyTest {

    public static void main(String[] args) {
        //目标对象
        Target target = new Target();

        //增强对象
        Advice advice = new Advice();

        //返回值 就是动态生成的代理对象 基于cglib
        //1、创建增强器
        Enhancer enhancer = new Enhancer();
        //2、设置父类（目标）
        enhancer.setSuperclass(Target.class);
        //3、设置回调
        enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
            advice.before(); //执行前置
            Object invoke = method.invoke(target, objects);//执行目标
            advice.afterReturning(); //执行后置
            return invoke;
        });
        //4、创建代理对象
        Target proxy = (Target) enhancer.create();

        proxy.save();
    }

}
